package com.wzxy.medical_backed.controller;

import com.wzxy.medical_backed.entity.Reimbursement;
import com.wzxy.medical_backed.entity.Result;
import com.wzxy.medical_backed.service.InsuredService;
import com.wzxy.medical_backed.service.ReimbursementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/reimbursement")
public class ReimbursementController {

    @Autowired
    private ReimbursementService reimbursementService;

    @Autowired
    private InsuredService insuredService;

    @GetMapping("/list")
    public Result<List<Reimbursement>> list() {
        List<Reimbursement> list = reimbursementService.list();
        for (Reimbursement reimbursement : list) {
            Double calculatedAmount = reimbursementService.calculateReimbursementAmount(
                    reimbursement.getTotalCost(), reimbursement.getMedicalName(), reimbursement.getDate());
            reimbursement.setReimbursementAmount(calculatedAmount);
        }
        return Result.success(list);
    }
    @PostMapping("/audit")
    public Result<String> audit(@RequestBody Map<String, Object> params) {
        Long id = Long.valueOf(params.get("id").toString());
        String isReimbursement = params.get("isReimbursement").toString();
        Reimbursement reimbursement = reimbursementService.getById(id);
        if (reimbursement == null) return Result.fail("未找到该报销申请");
        reimbursement.setIsReimbursement(isReimbursement);
        boolean updated = reimbursementService.updateById(reimbursement);
        return updated ? Result.success("审核状态更新成功") : Result.fail("审核状态更新失败");
    }

    @PostMapping("/remit")
    public Result<String> remit(@RequestBody Map<String, Object> params) {
        Long id = Long.valueOf(params.get("id").toString());
        String isRemit = params.get("isRemit").toString();
        Reimbursement reimbursement = reimbursementService.getById(id);
        if (reimbursement == null) return Result.fail("未找到该报销申请");
        reimbursement.setIsRemit(isRemit);
        boolean updated = reimbursementService.updateById(reimbursement);
        return updated ? Result.success("汇款状态更新成功") : Result.fail("汇款状态更新失败");
    }
    @GetMapping("/getstatics")
    public Result<Map<String, Integer>> getStatics() {
        List<Reimbursement> list = reimbursementService.list();
        int audited = 0, unaudited = 0;
        for (Reimbursement r : list) {
            if ("通过".equals(r.getIsReimbursement())) {
                audited++;
            } else {
                unaudited++;
            }
        }
        Map<String, Integer> map = new HashMap<>();
        map.put("已审核", audited);
        map.put("未审核", unaudited);
        return Result.success(map);
    }
    /**
     *
     * 新增报销申请
     * @param reimbursement 报销申请实体
     * @return 操作结果
     */
    @PostMapping("/add")
    public Result<String> add(@RequestBody Reimbursement reimbursement) {
        reimbursement.setIsReimbursement("未审核");
        reimbursement.setIsRemit("未汇款");
        Double calculatedAmount = reimbursementService.calculateReimbursementAmount(
                reimbursement.getTotalCost(), reimbursement.getMedicalName(), reimbursement.getDate());
        reimbursement.setReimbursementAmount(calculatedAmount);

        boolean save = reimbursementService.save(reimbursement);
        return save ? Result.success("新增报销申请成功") : Result.fail("新增报销申请失败");
    }
    }

